
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Consumer && Producer · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="operator.html" />
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    快速入门
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../guides/project.html">
            
                <a href="../guides/project.html">
            
                    
                    项目结构和初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../guides/api.html">
            
                <a href="../guides/api.html">
            
                    
                    API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../guides/doc.html">
            
                <a href="../guides/doc.html">
            
                    
                    生成 API 文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../guides/client.html">
            
                <a href="../guides/client.html">
            
                    
                    生成客户端
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.3.1" data-path="cp.html">
            
                <a href="cp.html">
            
                    
                    Consumer && Producer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="operator.html">
            
                <a href="operator.html">
            
                    
                    Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="filter.html">
            
                <a href="filter.html">
            
                    
                    Filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="prefab.html">
            
                <a href="prefab.html">
            
                    
                    Prefab
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="modifier.html">
            
                <a href="modifier.html">
            
                    
                    Modifier
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="middleware.html">
            
                <a href="middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="context.html">
            
                <a href="context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="source.html">
            
                <a href="source.html">
            
                    
                    Parameter Generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="destination.html">
            
                <a href="destination.html">
            
                    
                    Destination Handler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="error.html">
            
                <a href="error.html">
            
                    
                    错误包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="method.html">
            
                <a href="method.html">
            
                    
                    方法包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="configurer.html">
            
                <a href="configurer.html">
            
                    
                    配置器机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="plugin.html">
            
                <a href="plugin.html">
            
                    
                    插件机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="clients.html">
            
                <a href="clients.html">
            
                    
                    多客户端整合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    插件
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../plugins/logger.html">
            
                <a href="../plugins/logger.html">
            
                    
                    系统日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../plugins/reqlog.html">
            
                <a href="../plugins/reqlog.html">
            
                    
                    请求日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../plugins/metrics.html">
            
                <a href="../plugins/metrics.html">
            
                    
                    监控指标插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../plugins/tracing.html">
            
                <a href="../plugins/tracing.html">
            
                    
                    请求追踪插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../plugins/profiling.html">
            
                <a href="../plugins/profiling.html">
            
                    
                    性能分析插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../plugins/version.html">
            
                <a href="../plugins/version.html">
            
                    
                    版本信息插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../plugins/healthcheck.html">
            
                <a href="../plugins/healthcheck.html">
            
                    
                    健康检查插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    框架开发者指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../topics/start.html">
            
                <a href="../topics/start.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../topics/log.html">
            
                <a href="../topics/log.html">
            
                    
                    log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../topics/errors.html">
            
                <a href="../topics/errors.html">
            
                    
                    errors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../topics/definition.html">
            
                <a href="../topics/definition.html">
            
                    
                    definition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../topics/method.html">
            
                <a href="../topics/method.html">
            
                    
                    definition/method
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../topics/converter.html">
            
                <a href="../topics/converter.html">
            
                    
                    operators/converter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../topics/validator.html">
            
                <a href="../topics/validator.html">
            
                    
                    operators/validator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="../topics/router.html">
            
                <a href="../topics/router.html">
            
                    
                    service/router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="../topics/service.html">
            
                <a href="../topics/service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="../topics/nirvana.html">
            
                <a href="../topics/nirvana.html">
            
                    
                    nirvana
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="../topics/config.html">
            
                <a href="../topics/config.html">
            
                    
                    config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="../topics/rest.html">
            
                <a href="../topics/rest.html">
            
                    
                    rest
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="../topics/utils.html">
            
                <a href="../topics/utils.html">
            
                    
                    utils
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="../topics/command.html">
            
                <a href="../topics/command.html">
            
                    
                    cmd/nirvana
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Consumer && Producer</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="consumer-and-producer">Consumer and Producer</h1>
<p>&#x5305;&#x8DEF;&#x5F84;: <code>github.com/caicloud/nirvana/service</code></p>
<p>&#x5728; HTTP &#x8BF7;&#x6C42;&#x4E2D;&#xFF0C;Content-Type &#x8BF4;&#x660E;&#x4E86;&#x8BF7;&#x6C42;&#x548C;&#x54CD;&#x5E94;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;&#x4E3A;&#x4E86;&#x6839;&#x636E; Content-Type &#x81EA;&#x52A8;&#x5904;&#x7406;&#x6570;&#x636E;&#x8F6C;&#x6362;&#xFF0C;Nirvana &#x63D0;&#x4F9B;&#x4E86; Consumer &#x548C; Producer &#x63A5;&#x53E3;&#x3002;&#x5176;&#x4E2D; Consumer &#x7528;&#x4E8E;&#x5C06;&#x8BF7;&#x6C42;&#x4F53;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x8F6C;&#x6362;&#x4E3A;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x9700;&#x8981;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x800C; Producer &#x5219;&#x8D1F;&#x8D23;&#x5C06;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x5199;&#x5165;&#x5230;&#x54CD;&#x5E94;&#x4F53;&#x4E2D;&#x3002; </p>
<p>Nirvana &#x9ED8;&#x8BA4;&#x63D0;&#x4F9B;&#x7684; Consumers&#xFF1A;</p>
<table>
<thead>
<tr>
<th>Content-Type</th>
<th>&#x63CF;&#x8FF0;</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>&#x7A7A;&#x7684; Content-Type &#x901A;&#x5E38;&#x5BF9;&#x5E94;&#x4E8E; GET &#x4E4B;&#x7C7B;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x56E0;&#x6B64;&#x4E0D;&#x80FD;&#x8F6C;&#x6362;&#x4E3A;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;</td>
</tr>
<tr>
<td>text/plain</td>
<td>&#x53EA;&#x80FD;&#x751F;&#x6210; string &#x548C; []byte &#x7C7B;&#x578B;</td>
</tr>
<tr>
<td>application/json</td>
<td>&#x5982;&#x679C;&#x63A5;&#x6536;&#x7C7B;&#x578B;&#x662F; string &#x548C; []byte&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x5C06;&#x6570;&#x636E;&#x8F6C;&#x6362;&#x4E3A;&#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x578B;&#x3002;&#x5BF9;&#x4E8E;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#xFF0C;&#x4F7F;&#x7528; json.Unmarshal &#x8FDB;&#x884C;&#x89E3;&#x6790;&#x3002;</td>
</tr>
<tr>
<td>application/xml</td>
<td>&#x5982;&#x679C;&#x63A5;&#x6536;&#x7C7B;&#x578B;&#x662F; string &#x548C; []byte&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x5C06;&#x6570;&#x636E;&#x8F6C;&#x6362;&#x4E3A;&#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x578B;&#x3002;&#x5BF9;&#x4E8E;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#xFF0C;&#x4F7F;&#x7528; xml.Unmarshal &#x8FDB;&#x884C;&#x89E3;&#x6790;&#x3002;</td>
</tr>
<tr>
<td>application/octet-stream</td>
<td>&#x53EA;&#x80FD;&#x751F;&#x6210; string &#x548C; []byte &#x7C7B;&#x578B;</td>
</tr>
<tr>
<td>application/x-www-form-urlencoded</td>
<td>&#x53EA;&#x80FD;&#x751F;&#x6210; string &#x548C; []byte &#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x8BF7;&#x6C42;&#x901A;&#x5E38;&#x4F1A;&#x88AB; Parse &#x5E76;&#x6210;&#x4E3A; Form &#x7C7B;&#x578B;&#xFF0C;&#x56E0;&#x6B64;&#x4E00;&#x822C;&#x4E0D;&#x8F6C;&#x6362;&#x4E3A;&#x5177;&#x4F53;&#x7C7B;&#x578B;&#x3002;</td>
</tr>
<tr>
<td>multipart/form-data</td>
<td>&#x53EA;&#x80FD;&#x751F;&#x6210; string &#x548C; []byte &#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x8BF7;&#x6C42;&#x901A;&#x5E38;&#x4F1A;&#x88AB; Parse &#x5E76;&#x6210;&#x4E3A; Form &#x6216; File &#x7C7B;&#x578B;&#xFF0C;&#x56E0;&#x6B64;&#x4E00;&#x822C;&#x4E0D;&#x8F6C;&#x6362;&#x4E3A;&#x5177;&#x4F53;&#x7C7B;&#x578B;&#x3002;</td>
</tr>
</tbody>
</table>
<p>Nirvana &#x9ED8;&#x8BA4;&#x63D0;&#x4F9B;&#x7684; Producers&#xFF1A;</p>
<table>
<thead>
<tr>
<th>Content-Type</th>
<th>&#x63CF;&#x8FF0;</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>&#x7A7A;&#x7684; Content-Type &#x901A;&#x5E38;&#x5BF9;&#x5E94;&#x4E8E; 204 &#x4E4B;&#x7C7B;&#x7684;&#x54CD;&#x5E94;&#xFF0C;&#x6CA1;&#x6709;&#x54CD;&#x5E94;&#x4F53;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x5199;&#x5165;&#x3002;</td>
</tr>
<tr>
<td>text/plain</td>
<td>&#x5982;&#x679C;&#x7C7B;&#x578B;&#x7B26;&#x5408; io.Reader &#x63A5;&#x53E3;&#x6216;&#x8005;&#x662F; string &#x548C; []byte&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x5C06;&#x6570;&#x636E;&#x5199;&#x5165;&#x5230;&#x54CD;&#x5E94;&#x3002;</td>
</tr>
<tr>
<td>application/json</td>
<td>&#x5982;&#x679C;&#x7C7B;&#x578B;&#x7B26;&#x5408; io.Reader &#x63A5;&#x53E3;&#x6216;&#x8005;&#x662F; string &#x548C; []byte&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x5C06;&#x6570;&#x636E;&#x5199;&#x5165;&#x5230;&#x54CD;&#x5E94;&#x3002;&#x5982;&#x679C;&#x662F;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x4F7F;&#x7528; json.Marshal &#x5C06;&#x6570;&#x636E;&#x5199;&#x5165;&#x5230;&#x54CD;&#x5E94;&#x3002;</td>
</tr>
<tr>
<td>application/xml</td>
<td>&#x5982;&#x679C;&#x7C7B;&#x578B;&#x7B26;&#x5408; io.Reader &#x63A5;&#x53E3;&#x6216;&#x8005;&#x662F; string &#x548C; []byte&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x5C06;&#x6570;&#x636E;&#x5199;&#x5165;&#x5230;&#x54CD;&#x5E94;&#x3002;&#x5982;&#x679C;&#x662F;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x4F7F;&#x7528; xml.Marshal &#x5C06;&#x6570;&#x636E;&#x5199;&#x5165;&#x5230;&#x54CD;&#x5E94;&#x3002;</td>
</tr>
<tr>
<td>application/octet-stream</td>
<td>&#x5982;&#x679C;&#x7C7B;&#x578B;&#x7B26;&#x5408; io.Reader &#x63A5;&#x53E3;&#x6216;&#x8005;&#x662F; string &#x548C; []byte&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x5C06;&#x6570;&#x636E;&#x5199;&#x5165;&#x5230;&#x54CD;&#x5E94;&#x3002;</td>
</tr>
</tbody>
</table>
<h2 id="&#x6DFB;&#x52A0;-consumer-&#x548C;-producer">&#x6DFB;&#x52A0; Consumer &#x548C; Producer</h2>
<p>&#x5728;&#x4E1A;&#x52A1;&#x7684;&#x5B9E;&#x9645;&#x573A;&#x666F;&#x4E2D;&#xFF0C;&#x9ED8;&#x8BA4;&#x63D0;&#x4F9B;&#x7684; Consumers &#x548C; Producers &#x53EF;&#x80FD;&#x4E0D;&#x80FD;&#x6EE1;&#x8DB3;&#x5B9E;&#x9645;&#x4F7F;&#x7528;&#x9700;&#x6C42;&#x3002;&#x56E0;&#x6B64; Nirvana &#x7684; service &#x5305;&#x63D0;&#x4F9B;&#x4E86;&#x76F8;&#x5E94;&#x7684;&#x5DE5;&#x5177;&#x7528;&#x4E8E;&#x6CE8;&#x518C;&#x7528;&#x6237;&#x81EA;&#x5DF1;&#x7684; Consumer &#x548C; Producer&#x3002;</p>
<h3 id="&#x6CE8;&#x518C;-consumer">&#x6CE8;&#x518C; Consumer</h3>
<p>Consumer &#x9700;&#x8981;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Consumer handles specifically typed data from a reader and unmarshals it into an object.</span>
<span class="hljs-keyword">type</span> Consumer <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// ContentType returns a HTTP MIME type.</span>
    ContentType() <span class="hljs-keyword">string</span>
    <span class="hljs-comment">// Consume unmarshals data from r into v.</span>
    Consume(r io.Reader, v <span class="hljs-keyword">interface</span>{}) error
}
</code></pre>
<p>&#x5B9E;&#x73B0;&#x4E86;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x540E;&#xFF0C;&#x901A;&#x8FC7; service &#x7684;&#x6CE8;&#x518C;&#x65B9;&#x6CD5;&#x5373;&#x53EF;&#x6CE8;&#x518C; Consumer&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">if</span> err := service.RegisterConsumer(consumer); err != <span class="hljs-literal">nil</span> {
    log.Fatal(err)
}
</code></pre>
<p>&#x5728;&#x63A5;&#x6536;&#x5230; Content-Type &#x4E0E; consumer &#x4E00;&#x81F4;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x5E76;&#x4E14;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x9700;&#x8981;&#x4ECE;&#x8BF7;&#x6C42;&#x4E2D;&#x53D6;&#x5F97;&#x8BF7;&#x6C42;&#x4F53;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x4F1A;&#x8C03;&#x7528;&#x8FD9;&#x4E2A; Consumer &#x53BB;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#x5E76;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x3002;</p>
<h3 id="&#x6CE8;&#x518C;-producer">&#x6CE8;&#x518C; Producer</h3>
<p>Producer &#x9700;&#x8981;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Producer marshals an object to specifically typed data and write it into a writer.</span>
<span class="hljs-keyword">type</span> Producer <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// ContentType returns a HTTP MIME type.</span>
    ContentType() <span class="hljs-keyword">string</span>
    <span class="hljs-comment">// Produce marshals v to data and write to w.</span>
    Produce(w io.Writer, v <span class="hljs-keyword">interface</span>{}) error
}
</code></pre>
<p>&#x5B9E;&#x73B0;&#x4E86;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x540E;&#xFF0C;&#x901A;&#x8FC7; service &#x7684;&#x6CE8;&#x518C;&#x65B9;&#x6CD5;&#x5373;&#x53EF;&#x6CE8;&#x518C; Producer&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">if</span> err := service.RegisterProducer(producer); err != <span class="hljs-literal">nil</span> {
    log.Fatal(err)
}
</code></pre>
<p>&#x5728;&#x9700;&#x8981;&#x751F;&#x6210; Conetent-Type &#x4E8E; producer &#x4E00;&#x81F4;&#x7684;&#x54CD;&#x5E94;&#xFF0C;&#x5E76;&#x4E14;&#x4E1A;&#x52A1;&#x51FD;&#x6570;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x4F1A;&#x8C03;&#x7528;&#x8FD9;&#x4E2A; producer &#x5C06;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x8282;&#x6570;&#x636E;&#x5199;&#x5165;&#x5230;&#x54CD;&#x5E94;&#x4F53;&#x4E2D;&#x3002;</p>
<h3 id="&#x5FEB;&#x901F;&#x751F;&#x6210;-consumer-&#x548C;-producer-&#x7684;&#x5DE5;&#x5177;">&#x5FEB;&#x901F;&#x751F;&#x6210; Consumer &#x548C; Producer &#x7684;&#x5DE5;&#x5177;</h3>
<p>&#x901A;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5FEB;&#x901F;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B; Consumers &#x548C; Producers&#xFF0C;&#x5E76;&#x4E14;&#x4ED6;&#x4EEC;&#x7684;&#x884C;&#x4E3A;&#x548C; <code>application/octet-stream</code> &#x4E00;&#x81F4;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x5DE5;&#x5177;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-go">serializer := NewSimpleSerializer(contentType)

<span class="hljs-keyword">if</span> err := service.RegisterConsumer(serializer); err != <span class="hljs-literal">nil</span> {
    log.Fatal(err)
}

<span class="hljs-keyword">if</span> err := service.RegisterProducer(serializer); err != <span class="hljs-literal">nil</span> {
    log.Fatal(err)
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                
                <a href="operator.html" class="navigation navigation-next navigation-unique" aria-label="Next page: Operator">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Consumer && Producer","level":"1.3.1","depth":2,"next":{"title":"Operator","level":"1.3.2","depth":2,"path":"concepts/operator.md","ref":"concepts/operator.md","articles":[]},"previous":{"title":"概念","level":"1.3","depth":1,"ref":"","articles":[{"title":"Consumer && Producer","level":"1.3.1","depth":2,"path":"concepts/cp.md","ref":"concepts/cp.md","articles":[]},{"title":"Operator","level":"1.3.2","depth":2,"path":"concepts/operator.md","ref":"concepts/operator.md","articles":[]},{"title":"Filter","level":"1.3.3","depth":2,"path":"concepts/filter.md","ref":"concepts/filter.md","articles":[]},{"title":"Prefab","level":"1.3.4","depth":2,"path":"concepts/prefab.md","ref":"concepts/prefab.md","articles":[]},{"title":"Modifier","level":"1.3.5","depth":2,"path":"concepts/modifier.md","ref":"concepts/modifier.md","articles":[]},{"title":"Middleware","level":"1.3.6","depth":2,"path":"concepts/middleware.md","ref":"concepts/middleware.md","articles":[]},{"title":"Context","level":"1.3.7","depth":2,"path":"concepts/context.md","ref":"concepts/context.md","articles":[]},{"title":"Parameter Generator","level":"1.3.8","depth":2,"path":"concepts/source.md","ref":"concepts/source.md","articles":[]},{"title":"Destination Handler","level":"1.3.9","depth":2,"path":"concepts/destination.md","ref":"concepts/destination.md","articles":[]},{"title":"错误包","level":"1.3.10","depth":2,"path":"concepts/error.md","ref":"concepts/error.md","articles":[]},{"title":"方法包","level":"1.3.11","depth":2,"path":"concepts/method.md","ref":"concepts/method.md","articles":[]},{"title":"配置器机制","level":"1.3.12","depth":2,"path":"concepts/configurer.md","ref":"concepts/configurer.md","articles":[]},{"title":"插件机制","level":"1.3.13","depth":2,"path":"concepts/plugin.md","ref":"concepts/plugin.md","articles":[]},{"title":"多客户端整合","level":"1.3.14","depth":2,"path":"concepts/clients.md","ref":"concepts/clients.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*"},"file":{"path":"concepts/cp.md","mtime":"2019-08-06T09:48:58.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-20T10:33:16.586Z"},"basePath":"..","book":{"language":"zh-hans"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

